package provider;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import tools.StringUtil;


public class MapleDataTool {
    public static String getString(MapleData data) {
        /*  14 */
        if (data == null || data.getData() == null) {
            /*  15 */
            return "";
        }
        /*  17 */
        if (data.getData() instanceof String) {
            /*  18 */
            return (String) data.getData();
        }
        /*  20 */
        return String.valueOf(getLong(data));
    }

    public static String getString(MapleData data, String def) {
        /*  24 */
        if (data == null || data.getData() == null)
            /*  25 */ return def;
        /*  26 */
        if (data.getType() == MapleDataType.STRING || data.getData() instanceof String) {
            /*  27 */
            String ret = (String) data.getData();
            /*  28 */
            ret = ret.replace("&lt;", "<");
            /*  29 */
            ret = ret.replace("&amp;lt;", "<");
            /*  30 */
            ret = ret.replace("&gt;", ">");
            /*  31 */
            return ret;
        }
        /*  33 */
        return String.valueOf(getInt(data));
    }


    public static String getString(String path, MapleData data) {
        /*  38 */
        return getString(data.getChildByPath(path));
    }

    public static String getString(String path, MapleData data, String def) {
        /*  42 */
        return getString((data == null || data.getChildByPath(path) == null) ? null : data.getChildByPath(path), def);
    }

    public static double getDouble(MapleData data) {
        /*  46 */
        return ((Double) data.getData()).doubleValue();
    }

    public static float getFloat(MapleData data) {
        /*  50 */
        return ((Float) data.getData()).floatValue();
    }

    public static float getFloat(MapleData data, float def) {
        /*  54 */
        if (data == null || data.getData() == null) {
            /*  55 */
            return def;
        }
        /*  57 */
        return ((Float) data.getData()).floatValue();
    }


    public static int getInt(MapleData data) {
        /*  64 */
        int value = 0;
        /*  65 */
        if (data.getData() instanceof Integer) {
            /*  66 */
            value = ((Integer) data.getData()).intValue();
        } else {
            /*  68 */
            value = getShort(data);
        }
        /*  70 */
        return value;
    }


    public static short getShort(MapleData data) {
        /*  75 */
        String type = data.getType().name();
        /*  76 */
        if (null != type) {
            /*  77 */
            switch (type) {
                case "SHORT":
                case "INT":
                    /*  80 */
                    return ((Short) data.getData()).shortValue();
                case "STRING":
                    /*  82 */
                    return (short) Integer.parseInt(getString(data));
            }


        }
        /*  88 */
        return 0;
    }

    public static int getInt(MapleData data, int def) {
        /*  92 */
        if (data == null || data.getData() == null) {
            /*  93 */
            return def;
        }
        /*  95 */
        if (data.getType() == MapleDataType.STRING) {
            /*  96 */
            String data_ = getString(data);
            /*  97 */
            if (data_.isEmpty()) {
                /*  98 */
                data_ = "0";
            }
            /* 100 */
            return Integer.parseInt(data_);
            /* 101 */
        }
        if (data.getType() == MapleDataType.SHORT) {
            /* 102 */
            return ((Short) data.getData()).shortValue();
        }
        //PIAOGX
        if(Long.parseLong(data.getData().toString())>Integer.MAX_VALUE){

            return Integer.MAX_VALUE;
        }
        return ((Integer) data.getData()).intValue();
    }


    public static int getInt(String path, MapleData data) {
        /* 110 */
        return getInt(data.getChildByPath(path));
    }

    public static int getIntConvert(MapleData data) {
        /* 114 */
        if (data.getType() == MapleDataType.STRING) {
            /* 115 */
            return Integer.parseInt(getString(data));
        }
        /* 117 */
        return getInt(data);
    }


    public static int getIntConvert(String path, MapleData data) {
        /* 122 */
        MapleData d = data.getChildByPath(path);
        /* 123 */
        if (d.getType() == MapleDataType.STRING) {
            /* 124 */
            return Integer.parseInt(getString(d));
        }
        /* 126 */
        return getInt(d);
    }


    public static int getInt(String path, MapleData data, int def) {
        /* 131 */
        if (data == null) {
            /* 132 */
            return def;
        }
        /* 134 */
        return getInt(data.getChildByPath(path), def);
    }

    public static int getIntConvert(String path, MapleData data, int def) {
        /* 138 */
        if (data == null) {
            /* 139 */
            return def;
        }
        /* 141 */
        return getIntConvert(data.getChildByPath(path), def);
    }

    public static int getIntConvert(MapleData d, int def) {
        /* 145 */
        if (d == null) {
            /* 146 */
            return def;
        }
        /* 148 */
        if (d.getType() == MapleDataType.STRING) {
            /* 149 */
            String dd = getString(d);
            /* 150 */
            if (dd.endsWith("%")) {
                /* 151 */
                dd = dd.substring(0, dd.length() - 1);
            }
            try {
                /* 154 */
                return Integer.parseInt(dd);
                /* 155 */
            } catch (NumberFormatException nfe) {
                /* 156 */
                return def;
            }
        }
        /* 159 */
        return getInt(d, def);
    }


    public static long getLong(MapleData data) {
        long value;
        /* 165 */
        if (data.getData() instanceof Long) {
            /* 166 */
            value = ((Long) data.getData()).longValue();
        } else {
            /* 168 */
            value = getInt(data);
        }
        /* 170 */
        return value;
    }

    public static long getLong(String path, MapleData data) {
        /* 174 */
        return getLong(data.getChildByPath(path));
    }

    public static long getLong(MapleData data, long def) {
        try {
            /* 179 */
            return getLong(data);
            /* 180 */
        } catch (Exception ex) {
            /* 181 */
            return def;
        }
    }

    public static long getLong(String path, MapleData data, long def) {
        try {
            /* 187 */
            return getLong(data.getChildByPath(path));
            /* 188 */
        } catch (Exception ex) {
            /* 189 */
            return def;
        }
    }

    public static long getLongConvert(MapleData data) {
        /* 194 */
        if (data.getType() == MapleDataType.STRING) {
            /* 195 */
            String dd = getString(data);
            /* 196 */
            if (dd.endsWith("%")) {
                /* 197 */
                dd = dd.substring(0, dd.length() - 1);
            }
            /* 199 */
            return Long.parseLong(dd);
        }
        /* 201 */
        return getLong(data);
    }


    public static long getLongConvert(String path, MapleData data) {
        /* 206 */
        return getLongConvert(data.getChildByPath(path));
    }

    public static long getLongConvert(MapleData d, long def) {
        try {
            /* 211 */
            return getLongConvert(d);
            /* 212 */
        } catch (Exception ex) {
            /* 213 */
            return def;
        }
    }

    public static long getLongConvert(String path, MapleData data, long def) {
        try {
            /* 219 */
            return getLongConvert(data.getChildByPath(path));
            /* 220 */
        } catch (Exception ex) {
            /* 221 */
            return def;
        }
    }

    public static BufferedImage getImage(MapleData data) {
        /* 226 */
        return ((MapleCanvas) data.getData()).getImage();
    }

    public static Point getPoint(MapleData data) {
        /* 230 */
        return (Point) data.getData();
    }

    public static Point getPoint(String path, MapleData data) {
        /* 234 */
        return getPoint(data.getChildByPath(path));
    }

    public static Point getPoint(String path, MapleData data, Point def) {
        /* 238 */
        MapleData pointData = data.getChildByPath(path);
        /* 239 */
        if (pointData == null) {
            /* 240 */
            return def;
        }
        /* 242 */
        return getPoint(pointData);
    }

    public static String getFullDataPath(MapleData data) {
        /* 246 */
        String path = "";
        /* 247 */
        MapleDataEntity myData = data;
        /* 248 */
        while (myData != null) {
            /* 249 */
            path = myData.getName() + "/" + path;
            /* 250 */
            myData = myData.getParent();
        }
        /* 252 */
        return path.substring(0, path.length() - 1);
    }

    public static Map<?, ?> getAllMapleData(MapleData data) {
        /* 256 */
        Map<Object, Object> ret = new HashMap<>();
        /* 257 */
        for (MapleData subdata : data) {
            /* 258 */
            switch (subdata.getName()) {


                case "src/org/mxd/icon":
                case "iconRaw":
                    /* 263 */
                    for (MapleData subdatum : subdata) {
                        /* 264 */
                        boolean isInLink = subdatum.getName().equals("_inlink");
                        /* 265 */
                        boolean isOutLink = subdatum.getName().equals("_outlink");

                        /* 267 */
                        if (isInLink || isOutLink) {
                            /* 268 */
                            int inlink = 0;
                            /* 269 */
                            String[] split = subdatum.getData().toString().replace(".img", "").split("/");
                            /* 270 */
                            for (int i = 0; i < split.length; i++) {
                                /* 271 */
                                if (((isInLink && i == 0) || (isOutLink && (i == 2 || i == 3))) && StringUtil.isNumber(split[i])) {
                                    /* 272 */
                                    inlink = Integer.valueOf(split[i]).intValue();
                                }
                            }
                            /* 275 */
                            if (inlink != 0) {
                                /* 276 */
                                ret.put(subdatum.getName(), Integer.valueOf(inlink));
                            }
                        }
                    }
                    continue;
            }
            /* 282 */
            ret.put(subdata.getName(), subdata.getChildren().isEmpty() ? subdata.getData() : getAllMapleData(subdata));
        }
        /* 284 */
        return ret;
    }
}


/* Location:              C:\PGX\Server.jar!\provider\MapleDataTool.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */